package vn.anhqn.englishsentensetranslator.utils;

import org.apache.http.protocol.HTTP;

import vn.anhqn.englishsentensetranslator.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Utils {

	public static Utils INSTANCE;

	public static Utils getIntance() {
		if (INSTANCE == null)
			INSTANCE = new Utils();
		return INSTANCE;
	}

	public static void hideSoftKeyboard(Activity pActivity) {
		InputMethodManager inputMethodManager = (InputMethodManager) pActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		View viewFocus = pActivity.getCurrentFocus();
		if (viewFocus != null) {
			inputMethodManager.hideSoftInputFromWindow(viewFocus.getWindowToken(), 0);
		}
	}

	public void setFontFromAsset(TextView view, String assetPath) {
		try {
			Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), assetPath);
			view.setTypeface(typeface);
		} catch (Exception e) {

		}
	}

	@SuppressLint("NewApi")
	public void copyTextToClipboad(Context context, String text, int idString) {
		if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
			android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
					.getSystemService(Context.CLIPBOARD_SERVICE);
			clipboard.setText(text);
		} else {
			ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
			ClipData clip = ClipData.newPlainText("text label", text);
			clipboard.setPrimaryClip(clip);
		}
		Toast.makeText(context, idString, Toast.LENGTH_SHORT).show();
	}

	public void startActivityChooser(Context context, String textMessage) {
		Intent i = new Intent(Intent.ACTION_SEND);
		i.putExtra(Intent.EXTRA_TEXT, textMessage);
		i.setType(HTTP.PLAIN_TEXT_TYPE);
		Intent chooser = Intent.createChooser(i, "Send Via...");
		if (chooser.resolveActivity(context.getPackageManager()) != null) {
			context.startActivity(chooser);
		}
	}

	// get device size
	public static DisplayMetrics getDeviceSize(Context pContext) {
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		if (pContext instanceof Activity) {
			((Activity) pContext).getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		} else {
			mDisplayMetrics = pContext.getResources().getDisplayMetrics();
		}
		if (mDisplayMetrics.widthPixels == 0 || mDisplayMetrics.heightPixels == 0) {
			((WindowManager) pContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(mDisplayMetrics);
		}
		return mDisplayMetrics;
	}

	public static <T extends View> T findViewById(final View pParentView, final int pIdView) {
		if (/* this != null && */pParentView != null) {
			return (T) pParentView.findViewById(pIdView);
		}
		return null;
	}

}
